Python Starter Guide: From Novice to Coder: Python Programming for Absolute Beginners: Your Gateway to Coding Success by Svekis Sebastian & Svekis Laurence

Python Starter Guide: From Novice to Coder: Python Programming for Absolute Beginners: Your Gateway to Coding Success by Svekis Sebastian & Svekis Laurence

Author:Svekis, Sebastian & Svekis, Laurence
Language: eng
Format: epub, pdf
Published: 2023-09-26T00:00:00+00:00


When you run this code, you will see the following output:

{}

{}

<class 'dict'>

<class 'dict'>

In summary, this code demonstrates how to create empty dictionaries in Python using both the dict() constructor and curly braces {}, and it also shows how to check the data type of a variable using the type() function.

You can also set values when the dictionary is being created by adding them in as name pair values and comma separating them.

dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}

print(dictionary1)

output:

{'first': 'Hello World', 'num': 300, 'boo': True}

To return a value from the dictionary use the dictionary variable name and within the square brackets the key name.

dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}

print(dictionary1['num'])

Output :

300

Using the dictionary and the key name within the square brackets new values can be assigned to the keys.

dictionary1 = {"first":"Hello World","num":100,"num":200,"num":300,"boo":True}

print(dictionary1['num'])

dictionary1['num'] = 1

print(dictionary1['num'])

dictionary1['first'] = 'Laurence'

dictionary1['last'] = 'Svekis'

print(dictionary1)

This Python code demonstrates the creation and manipulation of a dictionary. Let's break down each part of the code:

dictionary1 = {"first": "Hello World", "num": 100, "num": 200, "num": 300, "boo": True}: This line initializes a dictionary named dictionary1 with multiple key-value pairs. The keys are strings, and the values can be of different types. Note that the key "num" is repeated three times, which is not allowed in a dictionary. The later assignments to the same key will overwrite the earlier ones.

print(dictionary1['num']): This line prints the value associated with the key 'num' in the dictionary1. However, due to the repeated assignments, the value associated with 'num' is 300, so it will print 300.

dictionary1['num'] = 1: This line assigns a new value of 1 to the key 'num' in dictionary1. This overwrites the previous value of 300.

print(dictionary1['num']): This line prints the updated value associated with the key 'num', which is now 1. It will print 1.

dictionary1['first'] = 'Laurence': This line assigns a new value of 'Laurence' to the key 'first' in dictionary1. This overwrites the previous value 'Hello World'.

dictionary1['last'] = 'Svekis': This line adds a new key-value pair to dictionary1, where the key is 'last' and the value is 'Svekis'.

print(dictionary1): This line prints the entire contents of dictionary1 after the modifications. It will display the updated dictionary with the new values and key-value pair.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.